package pers.dawnyang.common.core.util.math;

import java.util.Random;

/**
 * @author dawn
 * @date 2021/03/30
 */
public class RandomUtil {

    /**
     * 获得N位随机字符
     * 
     * @param length
     * @return
     */
    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            // 获取ascii码中的字符 数字48-57 小写65-90 大写97-122
            int range = random.nextInt(75) + 48;
            range = range < 97 ? (range < 65 ? (range > 57 ? 114 - range : range) : (range > 90 ? 180 - range : range))
                : range;
            sb.append((char)range);
        }
        return sb.toString();
    }

    /**
     * 获得N位随机数字
     * 
     * @param n
     * @return
     */
    public static String getRandNumber(int n) {
        String code = "";
        int codeDb = 0;
        for (int i = 0; i < n; i++) {
            codeDb = (int)(Math.abs(Math.random()) * 9);
            code += codeDb + "";
        }
        return code;
    }

}
